<template>
	<div class="system-config-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="configDialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="键名" prop="key">
							<el-input v-model="state.ruleForm.key" placeholder="请输入键名" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="键值" prop="value">
							<el-input v-model="state.ruleForm.value" placeholder="请输入键值" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="系统内置">
							<el-switch v-model="state.ruleForm.type" inline-prompt active-value="0" inactive-value="1" active-text="否" inactive-text="是"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemConfigDialog">
import { reactive, ref, nextTick } from 'vue';
import constConfig from '/@/utils/constConfig';
import { configApi } from '/@/api/system/config';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const configDialogFormRef = ref();
const state = reactive({
	ruleForm: {} as ConfigType,
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	rules: {
		name: { required: true, message: "名称不能为空", trigger: "blur" },
		key: { required: true, message: "键名不能为空", trigger: "blur" },
		value: { required: true, message: "键值不能为空", trigger: "blur" },
	},
});

// 打开弹窗
const openDialog = (type: string, row: ConfigType) => {
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	state.ruleForm = row;
	if (type === constConfig.dialogType.add) {
		state.dialog.title = '新增参数配置';
		state.dialog.submitTxt = '新 增';
	} else {
		state.dialog.title = '修改参数配置';
		state.dialog.submitTxt = '修 改';
	}
	nextTick(() => {
		configDialogFormRef.value.clearValidate();
	});
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	configDialogFormRef.value.validate((valid: boolean) => {
		if (valid) {
			if (state.dialog.type === constConfig.dialogType.add) {
				configApi().save(state.ruleForm).then(() =>{
					closeDialog();
					emit('refresh');
				})
			} else {
				configApi().update(state.ruleForm).then(()=>{
					closeDialog();
					emit('refresh');
				})
			}
		}
	})
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
